package com.atguigu.realtime.sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.realtime.sugar.bean.*;
import com.atguigu.realtime.sugar.service.KwService;
import com.atguigu.realtime.sugar.service.ProductService;
import com.atguigu.realtime.sugar.service.ProvinceService;
import com.atguigu.realtime.sugar.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzc
 * @Date 2022/3/29 9:27
 */
@RestController
public class SugarController {
    
    
    @Autowired
    ProductService productService;
    
    @RequestMapping("/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        System.out.println(date);
        // 如果没有传递日期过来, date的默认值是0
        if (date == 0) { // 如果默认值0, 则给date赋值今天的日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        System.out.println(date);
    
        BigDecimal gmv = productService.gmv(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        
        result.put("data", gmv);
        
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/gmv/tm")
    public String gmvByTm(@RequestParam("date") int date) {
        // 如果没有传递日期过来, date的默认值是0
        if (date == 0) { // 如果默认值0, 则给date赋值今天的日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<Map<String, Object>> list = productService.gmvByTm(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        
        for (Map<String, Object> map : list) {
            Object tm_name = map.get("tm_name");
            categories.add(tm_name);
        }
        
        
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject obj = new JSONObject();
        
        obj.put("name", "商品品牌");
        JSONArray data1 = new JSONArray();
        
        for (Map<String, Object> map : list) {
            Object order_amount = map.get("order_amount");
            data1.add(order_amount);
        }
        
        obj.put("data", data1);
        
        series.add(obj);
        
        
        data.put("series", series);
        
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    @RequestMapping("/gmv/c3")
    public String gmvByC3(@RequestParam("date") int date) {
        // 如果没有传递日期过来, date的默认值是0
        if (date == 0) { // 如果默认值0, 则给date赋值今天的日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<Tm> list = productService.gmvByC3(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        
        JSONArray data = new JSONArray();
        
        for (Tm tm : list) {
            JSONObject obj = new JSONObject();
            
            obj.put("name", tm.getCategory3_name());
            obj.put("value", tm.getOrder_amount());
            
            data.add(obj);
            
        }
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/gmv/spu")
    public String gmvBySpu(@RequestParam("date") int date) {
        // 如果没有传递日期过来, date的默认值是0
        if (date == 0) { // 如果默认值0, 则给date赋值今天的日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<Spu> list = productService.gmvBySpu(date);
        
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu 名");
        c1.put("id", "spu");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        
        
        data.put("columns", columns);
        
        
        JSONArray rows = new JSONArray();
        for (Spu spu : list) {
            JSONObject row = new JSONObject();
            row.put("spu", spu.getSpu_name());
            row.put("order_amount", spu.getOrder_amount());
            rows.add(row);
            
        }
        
        data.put("rows", rows);
        
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @Autowired
    ProvinceService provinceService;
    
    @RequestMapping("/province")
    public String province(@RequestParam("date") int date) {
        // 如果没有传递日期过来, date的默认值是0
        if (date == 0) { // 如果默认值0, 则给date赋值今天的日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<Province> list = provinceService.statProvince(date);
        
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray mapData = new JSONArray();
        
        for (Province province : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
            
        }
        
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @Autowired
    VisitorService visitorService;
    
    @RequestMapping("/visitor")
    public String visitor(@RequestParam("date") int date) {
        // 如果没有传递日期过来, date的默认值是0
        if (date == 0) { // 如果默认值0, 则给date赋值今天的日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<Visitor> list = visitorService.visitor(date);
        Map<Integer, Visitor> hourToVisit = new HashMap<Integer, Visitor>();
        for (Visitor visitor : list) {
            hourToVisit.put(visitor.getHour(), visitor);
        }
        System.out.println(hourToVisit);
        
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        // 一定是24个小时 0-23
        for (int i = 0; i < 24; i++) {
            categories.add(i);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        //第一个折线
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
        
        
        //第一个折线
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        //第一个折线
        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray ujData = new JSONArray();
        uj.put("data", ujData);
        series.add(uj);
        
        
        // pvData中, 一定是放入24个值
        for (int i = 0; i < 24; i++) {
            
            // i=0 hour是0点
            // 如果key不能存在, 则返回默认值
            Visitor visitor = hourToVisit.getOrDefault(i, new Visitor(i, 0L, 0L, 0L));
            System.out.println(visitor);
            pvData.add(visitor.getPv());
            uvData.add(visitor.getUv());
            ujData.add(visitor.getUj());
        }
        data.put("series", series);
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @Autowired
    KwService kwService;
    
    @RequestMapping("/kw")
    public String kw(@RequestParam("date") int date) {
        // 如果没有传递日期过来, date的默认值是0
        if (date == 0) { // 如果默认值0, 则给date赋值今天的日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<Kw> list = kwService.kw(date);
        
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (Kw kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getScore());
            
            data.add(obj);
        }
        
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
}
